﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 培训计划包含培训内容的管理（增删改查）
    /// </summary>
    public class TrainCourseController : ApiController
    {

        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        // POST: api/TrainCourse
        /// <summary>
        /// 添加培训计划及培训内容，如果培训计划对应的计划名称已经存在，那么就是更新培训计划
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainCourse/Add")]
        public IHttpActionResult Add(VM_TrainCourse_View view)
        {
            //string sResult = "";
            var model = view.vM_TrainCourse;
            //log.Info(JsonConvert.SerializeObject(model));

            Model.TrainCourse mTrainCourse = new Model.TrainCourse();
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                mTrainCourse.TPID = model.TPID;
                mTrainCourse.TCID = model.TCID;
                mTrainCourse.TCOID=new BLL.SysManage().GetMaxId("TrainCourse", "TCOID");
                mTrainCourse.StartDateTime = model.StartDateTime;
                mTrainCourse.EndDateTime = model.EndDateTime;
                mTrainCourse.TrainTeacherID = model.TrainTeacherID; 
                mTrainCourse.TrainTeacherName = model.TrainTeacherName;   
                mTrainCourse.Duration = model.Duration;
                mTrainCourse.AddUserID = LoginUser.UserInfo.UserID;
                mTrainCourse.AddUserName = LoginUser.UserInfo.UserName;
                mTrainCourse.AddDateTime = DateTime.Now;
                mTrainCourse.DepID = LoginUser.DepID; 
                mTrainCourse.IsUse = 1;
                mTrainCourse.IsCheck = 0;
                mTrainCourse.IsRelease = 0;// model.TPIsRelease;
                mTrainCourse.SendDepID = model.SendDepID??LoginUser.DepID.ToString();
                mTrainCourse.DepLevel = LoginUser.DepLevel;
                mTrainCourse.CRID = model.CRID;
                mTrainCourse.CRName = model.CRName;
                mTrainCourse.TCOName = model.TCOName;

                BLL.TrainCourse bllTrainCourse = new BLL.TrainCourse();
                BLL.TrainCourseFiles trainCourseFiles = new BLL.TrainCourseFiles();
                BLL.TrainCourseUser trainCourseUser = new BLL.TrainCourseUser();
                if (!bllTrainCourse.Exists(model.CRID,model.StartDateTime.ToString(),model.EndDateTime.ToString()))
                {
                    //培训计划不存在，添加新的培训计划
                    //增加主表
                    bllTrainCourse.Add(mTrainCourse);

                    #region 处理课程资料配置
                    var listCourseFileModel = new List<Model.TrainCourseFiles>();
                    var list1=GetFileList(view.vList1,mTrainCourse.TCOID,mTrainCourse.TPID,0);
                    var list2=GetFileList(view.vList2, mTrainCourse.TCOID, mTrainCourse.TPID, 1);
                    listCourseFileModel.AddRange(list1);
                    listCourseFileModel.AddRange(list2);
                    trainCourseFiles.AddList(listCourseFileModel);
                    #endregion

                    #region 处理课程人员配置
                    var listCourseUserModel = new List<Model.TrainCourseUser>();
                    if(view.UserIds!=null)
                    {
                        foreach (var item in view.UserIds)
                        {
                            Model.TrainCourseUser trainCourseUserObj = new Model.TrainCourseUser();
                            trainCourseUserObj.TPID = mTrainCourse.TPID;
                            trainCourseUserObj.TCOID = mTrainCourse.TCOID;
                            trainCourseUserObj.DeptID = LoginUser.DepID;
                            trainCourseUserObj.UserID = item;
                            listCourseUserModel.Add(trainCourseUserObj);
                        }
                        trainCourseUser.AddList(listCourseUserModel, mTrainCourse.TCOID, LoginUser.DepID);
                    }
                    else
                    {
                        trainCourseUser.AddList(listCourseUserModel, view.vM_TrainCourse.TCOID, LoginUser.DepID);
                    }
                    #endregion
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = 1;
                    resultMsg.Data = "培训计划增加成功！";
                }
                else
                {

                    //培训计划已经存在,获取培训计划对应的ID，此时是更新培训计划及培训计划的内容 
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "选择的教室在该时间段已有课程信息，请不要重复！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训计划增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainCourse/AddList")]
        public IHttpActionResult AddList(VM_TrainCourse_View view)
        {
            //string sResult = "";
            var model = view.vM_TrainCourse;
            //log.Info(JsonConvert.SerializeObject(model));

            Model.TrainCourse mTrainCourse = new Model.TrainCourse();
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                mTrainCourse.TPID = model.TPID;
                mTrainCourse.TCID = model.TCID;
                mTrainCourse.TCOID = new BLL.SysManage().GetMaxId("TrainCourse", "TCOID");
                mTrainCourse.StartDateTime = model.StartDateTime;
                mTrainCourse.EndDateTime = model.EndDateTime;
                mTrainCourse.TrainTeacherID = model.TrainTeacherID;
                mTrainCourse.TrainTeacherName = model.TrainTeacherName;
                mTrainCourse.Duration = model.Duration;
                mTrainCourse.AddUserID = LoginUser.UserInfo.UserID;
                mTrainCourse.AddUserName = LoginUser.UserInfo.UserName;
                mTrainCourse.AddDateTime = DateTime.Now;
                mTrainCourse.DepID = LoginUser.DepID;
                mTrainCourse.IsUse = 1;
                mTrainCourse.IsCheck = 0;
                mTrainCourse.IsRelease = 0;// model.TPIsRelease;
                mTrainCourse.SendDepID = model.SendDepID ?? LoginUser.DepID.ToString();
                mTrainCourse.DepLevel = LoginUser.DepLevel;
                mTrainCourse.CRID = model.CRID;
                mTrainCourse.CRName = model.CRName;
                mTrainCourse.TCOName = model.TCOName;

                BLL.TrainCourse bllTrainCourse = new BLL.TrainCourse();
                BLL.TrainCourseFiles trainCourseFiles = new BLL.TrainCourseFiles();
                BLL.TrainCourseUser trainCourseUser = new BLL.TrainCourseUser();
                if (!bllTrainCourse.Exists(model.CRID, model.StartDateTime.ToString(), model.EndDateTime.ToString()))
                {
                    //培训计划不存在，添加新的培训计划
                    //增加主表
                   var list=bllTrainCourse.AddList(mTrainCourse);

                    #region 处理课程资料配置
                    foreach(var item in list)
                    {
                        var listCourseFileModel = new List<Model.TrainCourseFiles>();
                        var list1 = GetFileList(view.vList1, item, mTrainCourse.TPID, 0);
                        var list2 = GetFileList(view.vList2, item, mTrainCourse.TPID, 1);
                        listCourseFileModel.AddRange(list1);
                        listCourseFileModel.AddRange(list2);
                        trainCourseFiles.AddList(listCourseFileModel);
                    }
                   
                    #endregion

                    #region 处理课程人员配置
                    var listCourseUserModel = new List<Model.TrainCourseUser>();
                    if (view.UserIds != null)
                    {
                        foreach (var i in list)
                        {
                            foreach (var item in view.UserIds)
                            {
                                Model.TrainCourseUser trainCourseUserObj = new Model.TrainCourseUser();
                                trainCourseUserObj.TPID = mTrainCourse.TPID;
                                trainCourseUserObj.TCOID = i;
                                trainCourseUserObj.DeptID = LoginUser.DepID;
                                trainCourseUserObj.UserID = item;
                                listCourseUserModel.Add(trainCourseUserObj);
                            }
                            trainCourseUser.AddList(listCourseUserModel, i, LoginUser.DepID);
                        }
                    }
                    else
                    {
                        trainCourseUser.AddList(listCourseUserModel, view.vM_TrainCourse.TCOID, LoginUser.DepID);
                    }
                    #endregion
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = 1;
                    resultMsg.Data = "培训计划增加成功！";
                }
                else
                {

                    //培训计划已经存在,获取培训计划对应的ID，此时是更新培训计划及培训计划的内容 
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "选择的教室在该时间段已有课程信息，请不要重复！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训计划增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        private List<Model.TrainCourseFiles> GetFileList(List<VM_Content> list,long TCOID,long TPID,int Mark)
        {
            var listCourseFileModel = new List<Model.TrainCourseFiles>();
            if (list==null)
            {
                return listCourseFileModel;
            }
            int num = 0;
            foreach (var item in list)
            {
                if (string.IsNullOrEmpty(item.CName) && item.files.Count == 0)
                { continue; }
                num++;
                
                if (item.files.Count > 0)
                {
                    foreach (var file in item.files)
                    {
                        if(file.id<0)
                        {
                            continue;
                        }
                        Model.TrainCourseFiles trainCourseFilesModel = new Model.TrainCourseFiles();
                        trainCourseFilesModel.TContent = item.CName;
                        trainCourseFilesModel.TCOID = TCOID;
                        trainCourseFilesModel.TPID = TPID;
                        trainCourseFilesModel.TCMark = Mark;
                        trainCourseFilesModel.OrderNum = num;
                        trainCourseFilesModel.TFID = file.id;
                        trainCourseFilesModel.TCDuration = item.TCDuration;
                        listCourseFileModel.Add(trainCourseFilesModel);
                    }
                }
            }
            return listCourseFileModel;
        }

        private void GetResultContentList(DataRow[] dataRowChild,Contents result,int mark)
        {
            int cnum = -1;
            VM_Content vM_ContentDetail = new VM_Content();
            int i = 0;int x = 0;
            foreach (var item in dataRowChild)
            {
                int num = item["OrderNum"] == null ? 0 : Convert.ToInt32(item["OrderNum"].ToString());
                if (cnum != num)
                {
                    if (cnum != -1) {
                        x++;
                        switch (mark)
                        {
                            case 0:
                                result.vList1.Add(vM_ContentDetail);
                                break;
                            case 1: result.vList2.Add(vM_ContentDetail); break;
                        }
                    }
                    vM_ContentDetail = new VM_Content();
                    vM_ContentDetail.files = new List<VM_ContentFile>();
                    cnum = num;
                }
                vM_ContentDetail.idx = x.ToString();
                vM_ContentDetail.CName = item["TContent"] == null ? "" : item["TContent"].ToString();
                vM_ContentDetail.TCDuration = item["TCDuration"] == null ? 0 : Convert.ToInt32(item["TCDuration"].ToString()==""?"0": item["TCDuration"].ToString());
                long tfid = item["TFID"] == null ? 0 : Convert.ToInt64(item["TFID"]);
                string tfname = item["TFName"] == null ? "" : item["TFName"].ToString();
                vM_ContentDetail.files.Add(new VM_ContentFile() { id = tfid, name = tfname });
                if (i== dataRowChild.Length-1)
                {
                    switch (mark)
                    {
                        case 0:
                            result.vList1.Add(vM_ContentDetail);
                            break;
                        case 1: result.vList2.Add(vM_ContentDetail); break;
                    }
                }
                i++;
            }
        }
        [HttpGet]
        [Route("api/TrainCourse/GetContent")]
        public IHttpActionResult GetContent(string TCOID)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                Contents contents = new Contents();
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourseFiles().GetListView(" a.TCOID='"+TCOID+"' ORDER by a.TCMark asc ");
                Contents result = new Contents();
                if (dsSearch != null)
                {
                    if (dsSearch.Tables.Count > 0)
                    {
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            DataRow[] dataRowChild = dsSearch.Tables[0].Select(string.Format(@"TCMark={0}",0));
                            DataRow[] dataRowChild2 = dsSearch.Tables[0].Select(string.Format(@"TCMark={0}", 1));
                            #region 1
                            result.vList1 = new List<VM_Content>();
                            GetResultContentList(dataRowChild, result,0);
                            GetResultContentList(dataRowChild2, result,1);
                            #endregion
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data=result;
                        }
                    }
                }
                //if (mTP != null && mTP.TPID != 0)
                //{
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                //    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                //    resultMsg.Data = mTP;
                //}
                //else
                //{
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                //    resultMsg.Info = "数据不存在！";
                //}
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }

        #region 主页 简要课程人员统计

        //人员学习情况统计(饼图)
        [HttpGet]
        [Route("api/TrainCourse/GetPersonStrudyReport")]
        public IHttpActionResult GetPersonStrudyReport()
        {
            string where = "";
            DateTime startTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-01");
            DateTime endTime = startTime.AddMonths(1);

            if (LoginUser.DepLevel==0)
            {
                where = "StartDateTime>='" + startTime + "' and StartDateTime<'" + endTime + "'";
            }
            else
            {
                where = "StartDateTime>='" + startTime + "' and StartDateTime<'" + endTime + "' and DepID='"+LoginUser.DepID+"'";
            }
            
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                string dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().GetCourseStudy(where);
                StudyInfo result = new StudyInfo();
                if (dsSearch != null&&dsSearch.IndexOf(",")>-1)
                {
                    var r = dsSearch.Split(',');
                    result.t =Convert.ToInt32(r[0]);
                    result.y = Convert.ToInt32(r[1]);
                    result.w = result.t-result.y;
                    if (result.w<0) { result.w = 0; }
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = result;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }

        //车间/车队 人员学习达标情况统计
        [HttpGet]
        [Route("api/TrainCourse/GetCourseStudyReaching")]
        public IHttpActionResult GetCourseStudyReaching()
        {
            string where = "";
            DateTime startTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-01");
            DateTime endTime = startTime.AddMonths(1);
            bool isDuan = false;
            if (LoginUser.DepLevel == 0)
            {
                isDuan = true;
                where = "StartDateTime>='" + startTime + "' and StartDateTime<'" + endTime + "'";
            }
            else
            {
                where = "StartDateTime>='" + startTime + "' and StartDateTime<'" + endTime + "' ";
            }

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseStudy> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().GetCourseStudyReaching(where, isDuan,LoginUser.DepID);
                StudyInfo result = new StudyInfo();
                if (dsSearch != null )
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion


        #region 查询统计  课程数量及培训内容统计  乘务员学习次数及不足4次的统计  记录查询

        #region 培训课程及内容详细 同i就
        #region 参加培训课程数量统计查询  车间
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountChejian")]
        public IHttpActionResult GetCourseCountChejian(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime=DateTime.Now;
            DateTime endTime=DateTime.Now;
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_Chejian(LoginUser.DepLevel, LoginUser.DepID,startTime,endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion
        #region 参加培训课程数量统计查询  车队
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountChedui")]
        public IHttpActionResult GetCourseCountChedui(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_Chedui(LoginUser.DepLevel, LoginUser.DepID, startTime, endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion
        #region 参加培训课程数量统计查询  乘务员
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountChengwuyuan")]
        public IHttpActionResult GetCourseCountChengwuyuan(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string chedui_Ids = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "chedui_Ids")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            chedui_Ids = item.Value;
                        }
                    }
                    
                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_Chengwuyuan(chedui_Ids,startTime, endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion
        #region 参加培训课程数量统计查询  乘务员培训内容
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountContent")]
        public IHttpActionResult GetCourseCountContent(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string User_Id = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "User_Id")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            User_Id = item.Value;
                        }
                    }

                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_Content(User_Id, startTime, endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion
        #region 未培训人员
        [HttpGet]
        [Route("api/TrainCourse/GetCourseStudyCourseuncommittedPerson")]
        public IHttpActionResult GetCourseStudyCourseuncommittedPerson(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string chedui_Ids = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "chedui_Ids")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            chedui_Ids = item.Value;
                        }
                    }

                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCoursePerson> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourse_uncommittedPerson(chedui_Ids, startTime, endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                    resultMsg.Count = dsSearch.Count;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion
        #endregion

        #region 参加学习次数统计  乘务员

        #region 乘务员学习课程次数
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountChengwuyuanStudyCount")]
        public IHttpActionResult GetCourseCountChengwuyuanStudyCount(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string personName = "";
            string chedui_Ids = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "chedui_Ids")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            chedui_Ids = item.Value;
                        }
                    }
                    if (item.Key == "personName")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            personName = item.Value;
                        }
                    }
                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_StudyCount(chedui_Ids, startTime, endTime, personName);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        #region 学习次数不足4次的乘务员
        [HttpGet]
        [Route("api/TrainCourse/GetCourseCountChengwuyuan_CountNo")]
        public IHttpActionResult GetCourseCountChengwuyuan_CountNo(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;

            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string chedui_Ids = ""; string personName = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "chedui_Ids")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            chedui_Ids = item.Value;
                        }
                    }
                    if (item.Key == "personName")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            personName = item.Value;
                        }
                    }


                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_CountNo(chedui_Ids, startTime, endTime, personName);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        #region 某乘务员 学习课程 及 次数
        [HttpGet]
        [Route("api/TrainCourse/GetCourseStudyCourseCount_ChengwuyuanCourseDetailCount")]
        public IHttpActionResult GetCourseStudyCourseCount_ChengwuyuanCourseDetailCount(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string User_Id = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "User_Id")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            User_Id = item.Value;
                        }
                    }

                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_ChengwuyuanCourseDetailCount(User_Id, startTime, endTime);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        #endregion

        #region 获取车队列表
        [HttpGet]
        [Route("api/TrainCourse/GetCheduiList")]
        public IHttpActionResult GetCheduiList()
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                Get_CheduiList(LoginUser.DepLevel,LoginUser.DepID);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        #region 获取车间列表
        [HttpGet]
        [Route("api/TrainCourse/GetChejianList")]
        public IHttpActionResult GetChejianList()
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                Get_ChejianList(LoginUser.DepLevel, LoginUser.DepID);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        #region 查询 学习了该课程的 乘务员  学习记录
        [HttpGet]
        [Route("api/TrainCourse/GetCourseStudyCourseCount_ChengwuyuanStudyCourseRecorder")]
        public IHttpActionResult GetCourseStudyCourseCount_ChengwuyuanStudyCourseRecorder(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string TCOID = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "TCOID")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            TCOID = item.Value;
                        }
                    }

                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                List<ReportCourseCount> dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().
                GetCourseStudyCourseCount_ChengwuyuanStudyCourseRecorder(TCOID);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch;
                    resultMsg.Count = dsSearch.Count;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
        #endregion

        
        #endregion


        /// <summary>
        /// 试卷查询
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainCourse/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "TrainTeacherID")
                        {if((item.Value!="")&&(item.Value!=null))
                            swhere += $" AND TrainTeacherID = '{item.Value}' ";
                        }
                        if (item.Key == "StartDateTime")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND StartDateTime >= '{item.Value}' ";
                        }
                        if (item.Key == "EndDateTime")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND EndDateTime <= '{item.Value}'";
                        }
                        if (item.Key == "IsCheck")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND IsCheck ={item.Value} ";
                        }
                        if (item.Key == "TCOName")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND TCOName ='{item.Value}' ";
                        }
                        
                        if (item.Key == "IsUse")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND IsUse ={item.Value} ";
                        }
                        if (item.Key== "self")
                        {
                            if ((item.Value != "") && (item.Value != null))
                            {
                                switch (item.Value)
                                {
                                    case "0"://列表页 ,当前部门,上级部门,审核，发布
                                        swhere += $" AND (( DepLevel<" + LoginUser.DepLevel + "  and IsCheck='1')";
                                        swhere += $" or ( DepLevel=" + LoginUser.DepLevel + " AND DepID=" + LoginUser.DepID + "))";
                                       //swhere += $" OR (SendDepID like '" + LoginUser.DepID.ToString() + "%' or SendDepID like '" + LoginUser.DepID.ToString() + ",%' or SendDepID like '," + LoginUser.DepID.ToString() + "%' ) ";
                                        break;
                                    case "1"://筛选页 [培训计划 筛选上级培训计划]
                                        swhere += $" AND ( DepLevel=" + LoginUser.DepPLevel + "  and IsCheck='1')";
                                        break;
                                    case "2"://筛选页 [筛选本部门计划  如：授课内容 筛选培训计划]
                                        swhere += $" AND DepID=" + LoginUser.DepID + "  and IsCheck='1' ";
                                        break;
                                }
                                
                            }
                        }
                    }
                }

                bool blExist = false;
                int rowCount = 0;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().GetListByPage(swhere, "startdatetime desc", startindex, endindex,out rowCount);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText()+exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训计划编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainCourse/Edit")]
        public IHttpActionResult Edit(VM_TrainCourse_View view)
        {
            Model.TrainCourse model = new Model.TrainCourse();
            log.Info(JsonConvert.SerializeObject(model));
            model.AddUserID = LoginUser.UserInfo.UserID;
            model.AddUserName = LoginUser.UserInfo.UserName;
            model.AddDateTime = DateTime.Now;
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                #region 赋值
                model.TPID = view.vM_TrainCourse.TPID;
                model.TCID = view.vM_TrainCourse.TCID;
                model.TCOID = view.vM_TrainCourse.TCOID;
                model.StartDateTime = view.vM_TrainCourse.StartDateTime;
                model.EndDateTime = view.vM_TrainCourse.EndDateTime;
                model.TrainTeacherID = view.vM_TrainCourse.TrainTeacherID;
                model.TrainTeacherName = view.vM_TrainCourse.TrainTeacherName;
                model.Duration = view.vM_TrainCourse.Duration;
                model.TCOName = view.vM_TrainCourse.TCOName;

                //model.AddUserID = LoginUser.UserInfo.UserID;
                //model.AddUserName = LoginUser.UserInfo.UserName;
                //model.AddDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //model.DepID = LoginUser.DepID;
                //model.IsUse = 1;
                //model.IsCheck = 0;
                //model.IsRelease = 0;// model.TPIsRelease;
                model.SendDepID =view.vM_TrainCourse.SendDepID ?? LoginUser.DepID.ToString();
                //model.DepLevel = LoginUser.DepPLevel;
                model.CRID = view.vM_TrainCourse.CRID;
                model.CRName = view.vM_TrainCourse.CRName;
                #endregion

                BLL.TrainCourseFiles trainCourseFiles = new BLL.TrainCourseFiles();
                BLL.TrainCourseUser trainCourseUser = new BLL.TrainCourseUser();
                string errorMsg = string.Empty;
                if (new IPR_StudyMeetingMrgSys.BLL.TrainCourse().Update(model,LoginUser.DepLevel,out errorMsg))
                {
                    if(string.IsNullOrEmpty(errorMsg))
                    {
                        var listCourseFileModel = new List<Model.TrainCourseFiles>();
                        var list1 = GetFileList(view.vList1, view.vM_TrainCourse.TCOID, view.vM_TrainCourse.TPID, 0);
                        var list2 = GetFileList(view.vList2, view.vM_TrainCourse.TCOID, view.vM_TrainCourse.TPID, 1);
                        listCourseFileModel.AddRange(list1);
                        listCourseFileModel.AddRange(list2);
                        trainCourseFiles.AddList(listCourseFileModel, model.TCOID);
                    }
                    #region 处理课程人员配置
                    var listCourseUserModel = new List<Model.TrainCourseUser>();
                    if(view.UserIds!=null)
                    {
                        foreach (var item in view.UserIds)
                        {
                            Model.TrainCourseUser trainCourseUserObj = new Model.TrainCourseUser();
                            trainCourseUserObj.TPID = view.vM_TrainCourse.TPID;
                            trainCourseUserObj.TCOID = view.vM_TrainCourse.TCOID;
                            trainCourseUserObj.DeptID = LoginUser.DepID;
                            trainCourseUserObj.UserID = item;
                            listCourseUserModel.Add(trainCourseUserObj);
                        }
                        trainCourseUser.AddList(listCourseUserModel, view.vM_TrainCourse.TCOID, LoginUser.DepID);
                    }
                    else
                    {
                        trainCourseUser.AddList(listCourseUserModel, view.vM_TrainCourse.TCOID, LoginUser.DepID);
                    }
                    #endregion

                    resultMsg.Info = "编辑成功";
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = errorMsg;
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训计划删除(根据人员工号)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainCourse/Del")]
        public IHttpActionResult Del(Model.TrainCourse model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                    if (new IPR_StudyMeetingMrgSys.BLL.TrainCourse().Delete(model.TPID))
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = "培训计划删除成功！";
                    }
                    else
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "培训计划删除失败！";
                    }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "培训计划删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训计划信息删除(根据文件工号),连带删除 课程人员配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainCourse/DelList")]
        public IHttpActionResult DelList(string delidlist)
        {
            log.Info(JsonConvert.SerializeObject(delidlist));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainCourse().DeleteList(delidlist))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训计划删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "删除成功，删除0行"; 
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "文件信息删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 培训计划发布
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainCourse/Release")]
        public IHttpActionResult ReleaseList(string TCOIDlist)
        {
            log.Info(JsonConvert.SerializeObject(TCOIDlist));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainCourse().Release(TCOIDlist))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训计划删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "删除成功，删除0行";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "文件信息删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 培训计划信息删除(根据文件工号)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainCourse/GetSingle")]
        public IHttpActionResult GetSingle(string TCOID)
        {
            log.Info(JsonConvert.SerializeObject(TCOID));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                Model.TrainCourse mTP = new IPR_StudyMeetingMrgSys.BLL.TrainCourse().GetModel( Int32.Parse( TCOID));
                if (mTP!=null&&mTP.TPID!=0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = mTP;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = "数据不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 更新计划的状态，包含(审核、取消审核、发布、取消发布、使用、未使用)
        /// value 为0时表示取消审核/发布/使用中，为1时表未取消审核/取消发布/未使用
        /// </summary>
        /// <param name="mark">标记码，审核：C / 发布：R / 使用：U</param>
        /// <param name="value">1/0</param>
        /// <param name="wherevalue">条件，使用主键字段值 </param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainCourse/UpdatePlanStatus")]
        public IHttpActionResult UpdatePlanStatus(string mark, string value, string wherevalue)
        {
            log.Info(JsonConvert.SerializeObject(mark + "=" + value));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                string tablename = "TrainCourse";
                string fieldname = "";
                string fieldvalue = "";
                string wherefield = "TCOID";
                if (mark == "C")
                {
                    fieldname = "IsCheck";
                }
                if (mark == "R")
                {
                    fieldname = "IsRelease";
                }
                if (mark == "U")
                {
                    fieldname = "IsUse";
                }
                fieldvalue = value;
                string ErrorMsg = string.Empty;
                new BLL.SysManage().ExecuteUpdate(tablename, fieldname, fieldvalue, wherefield, wherevalue, out ErrorMsg);
                if (string.IsNullOrEmpty(ErrorMsg))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "更新成功";
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = ErrorMsg;
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        // GET: api/TrainCourse
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/TrainCourse/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/TrainCourse
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/TrainCourse/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/TrainCourse/5
        public void Delete(int id)
        {
        }
    }
}